<%@ Page Language="VB" MasterPageFile="~/Default.master" ValidateRequest="false" Title="Untitled Page" %>
<%@ Import Namespace="SubSonic" %>
<%@ Import Namespace="ClubStarterKit.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

    Protected Sub UploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
        Dim albumid As Integer
                
        Dim o As Object = Request("albumid")
        If Not o Is Nothing Then
            albumid = CInt(o)
        End If
        Try     
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                ImageUtils.uploadTempImage(imgTitle.Text, albumid, FileUpload1.FileContent)
            ElseIf imageFileLocation = ImageUtils.ImageLocation.FileSystem Then
                ImageUtils.uploadTempImage(imgTitle.Text, albumid, FileUpload1.FileContent, Server.MapPath("~\TempUpload") & "\")
            End If

            ErrorLabel.Text = ""
            imgTitle.Text = ""
        Catch ex As Exception
            ErrorLabel.Text = "<p>Please select a file to upload<p>" & ex.Message
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Request("albumid") Is Nothing Then
            Response.Redirect("~/photo/list.aspx")
        End If
    End Sub


    Protected Sub UploadAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)      
        Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
    
        If imageFileLocation = ImageUtils.ImageLocation.Database Then
            Dim col As New TempImagesDBCollection()
            col.LoadAndCloseReader(TempImagesDB.FetchByParameter(TempImagesDB.Columns.Album, Request.QueryString("albumid")))
            
            Dim item As TempImagesDB
            For Each item In col
                ClubStarterKit.Data.Images.Insert(item.Title, ImageUtils.MakeThumb(item.Img), item.Thumb, item.Img, item.Album, "")
                ClubStarterKit.Data.TempImagesDB.Delete(TempImagesDB.Columns.Id, item.Id)
            Next
        Else
            Dim col As New TempImagesFileCollection()
            col.LoadAndCloseReader(TempImagesFile.FetchByParameter(TempImagesFile.Columns.Album, Request.QueryString("AlbumID")))
        
            Dim item As TempImagesFile
            For Each item In col
                Dim path As String = Server.MapPath("~\Pictures") & "\"
                Dim fileName As String = HttpContext.Current.Request.PhysicalApplicationPath & "tempupload\" & item.Filename
                
                Dim data As New FileStream(fileName, FileMode.Open, FileAccess.Read)
                
                ImageUtils.uploadImage(item.Title, item.Album, data, path)
                
                ClubStarterKit.Data.TempImagesFile.Delete(TempImagesFile.Columns.Id, item.Id)
                
                data.Close()
                
                Dim f As New FileInfo(fileName)
                f.Delete()
                
                Dim thumbf As New FileInfo(HttpContext.Current.Request.PhysicalApplicationPath & "tempupload\tmb" & item.Filename)
                thumbf.Delete()
            Next
        End If
        
        Response.Redirect("~/photo/contents.aspx?albumid=" & Request("albumid"))
    End Sub

    Protected Sub DeleteAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim imageFileLocation As ImageUtils.ImageLocation = ImageUtils.GetLocation()
    
        If imageFileLocation = ImageUtils.ImageLocation.Database Then
            Dim col As New TempImagesDBCollection()
            col.LoadAndCloseReader(TempImagesDB.FetchByParameter(TempImagesDB.Columns.Album, Request.QueryString("albumid")))
        
            Dim item As TempImagesDB
        
            For Each item In col
                ClubStarterKit.Data.TempImagesDB.Delete(TempImagesDB.Columns.Id, item.Id)
            Next
        Else
            Dim col As New TempImagesFileCollection()
            col.LoadAndCloseReader(TempImagesFile.FetchByParameter(TempImagesFile.Columns.Album, Request.QueryString("AlbumID")))
        
            Dim item As TempImagesFile
            For Each item In col
                ClubStarterKit.Web.Photos.DeleteTempImage(item)
            Next
        End If
        
        Response.Redirect("~/photo/contents.aspx?albumid=" & Request("albumid"))
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="body">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
    <div class="fullwidth" id="UploadDiv">   
                <h2>
                Upload Photo</h2>
            <asp:Label ID="ErrorLabel" runat="server" Text="" />
            <table>
                <tr>
                    <td class="formlabel">
                        File:</td>
                    <td>
                        <asp:FileUpload ID="FileUpload1" runat="server" CssClass="txtfield" /></td>
                </tr>
                <tr>
                    <td class="formlabel">
                        Image title:</td>
                    <td>
                        <asp:TextBox ID="imgTitle" runat="server" CssClass="txtfield" />
                    </td>
                </tr>
            </table>
            <div class="actionbuttons">
                <Club:RolloverButton ID="UploadFile" runat="server" Text="Upload" OnClick="UploadFile_Click" />
            </div>
        &nbsp;
        
    </div>
    
    <div class="fullwidth" id="ListDiv">
        <asp:GridView ID="Grid" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" DataSourceID="TempFile" GridLines="None" ShowHeader="False" Width="379px">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Image ID="gridimg" runat="server" ImageUrl='<%# "~/usercontrols/TempImageFetch.ashx?imageid=" & Eval("id").ToString() %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="imgttl" runat="server" Text='<%# Eval("Title") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EmptyDataTemplate>
                There are no images in the upload queue.
            </EmptyDataTemplate>
        </asp:GridView>
        <br />
        <Club:RolloverButton id="UploadAll" runat="server" Text="Upload All" OnClick="UploadAll_Click" /> <Club:RolloverButton id="DeleteAll" runat="server" Text="Delete All" OnClick="DeleteAll_Click" />
    </div>
    <asp:ObjectDataSource ID="TempFile" runat="server" DeleteMethod="DeleteTempImage" SelectMethod="LoadTempImages" TypeName="ClubStarterKit.Web.Photos">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="albumid" QueryStringField="albumid" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
</div>
</asp:Content>

